package client;

import customer.CustomerGrpc;
import customer.CustomerOuterClass;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class CustomerClient {
    private final ManagedChannel channel;
    private final CustomerGrpc.CustomerBlockingStub blockingStub;
    public CustomerClient(String host, int port) {
        this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
    }

    private CustomerClient(ManagedChannelBuilder<?> channelBuilder) {
        channel = channelBuilder.build();
        blockingStub = CustomerGrpc.newBlockingStub(channel);
    }

    public void shutdown() throws InterruptedException {
        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    }

    public CustomerOuterClass.CustomerResponse createCustomer(CustomerOuterClass.CustomerRequest request) {
        return blockingStub.createCustomer(request);
    }

    public Iterator<CustomerOuterClass.CustomerRequest> getAll(CustomerOuterClass.CustomerFilter filter) {
        return blockingStub.getCustomers(filter);
    }

    public static void main(String[] args) {
        CustomerClient client = new CustomerClient("localhost", 50051);

        // write test
        CustomerOuterClass.CustomerRequest.Address newAddr = CustomerOuterClass.CustomerRequest.Address.newBuilder()
                .setStreet("ceshi")
                .setCity("ceshi")
                .setState("ceshi")
                .setZip("nice")
                .setIsShippingAddress(false)
                .build();

        CustomerOuterClass.CustomerRequest fu = CustomerOuterClass.CustomerRequest.newBuilder()
                .setId(103)
                .setName("fu")
                .setEmail("fufdfd@qq.com")
                .setPhone("321654")
                .addAddresses(newAddr)
                .build();
        CustomerOuterClass.CustomerResponse resp = client.createCustomer(fu);
        if (resp.getSuccess()) {
            System.out.println(resp.getId());
            System.out.println("success");
        }


        // read test
        CustomerOuterClass.CustomerFilter request = CustomerOuterClass.CustomerFilter.newBuilder()
                .setKeyword("")
                .build();
        Iterator<CustomerOuterClass.CustomerRequest> all = client.getAll(request);
        while (all.hasNext()) {
            CustomerOuterClass.CustomerRequest next = all.next();
            System.out.println("id: " + next.getId());
            System.out.println("name: " + next.getName());
            System.out.println("" + next.getEmail());
            System.out.println("" + next.getPhone());
            List<CustomerOuterClass.CustomerRequest.Address> addressesList = next.getAddressesList();
            for (CustomerOuterClass.CustomerRequest.Address addr : addressesList) {
                System.out.print("    ");
                System.out.println(addr.getState() + "   " + addr.getCity() + "   " + addr.getStreet() + "   " + addr.getZip());
            }
        }


    }

}
